// EditLessonPage.cpp : implementation file
//

#include "stdafx.h"
#include "SelectLesson.h"
#include "EditLessonPage.h"
#include "SelectLessonView.h"
#include "LessonInformationset.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEditLessonPage property page

IMPLEMENT_DYNCREATE(CEditLessonPage, CPropertyPage)

CEditLessonPage::CEditLessonPage() : CPropertyPage(CEditLessonPage::IDD)
{
	//{{AFX_DATA_INIT(CEditLessonPage)
	m_LessonName = _T("");
	m_LessonNumber = 0;
	m_MaxNumber = 0;
	m_TeacherNumber = 0;
	m_BeginDate = _T("");
	//}}AFX_DATA_INIT
}

CEditLessonPage::~CEditLessonPage()
{
}
BOOL CEditLessonPage::OnApply()
{
    TRACE("CEditLessonPage::OnApply\n");
    return TRUE;
}

BOOL CEditLessonPage::OnCommand(WPARAM wParam, LPARAM lParam)
{
    SetModified(TRUE);
    return CPropertyPage::OnCommand(wParam, lParam);
}

void CEditLessonPage::DoDataExchange(CDataExchange* pDX)
{
    TRACE("Entering CEditLessonPage::DoDataExchange -- %d\n",
          pDX->m_bSaveAndValidate);

	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEditLessonPage)
	DDX_Control(pDX, IDC_TEACHERNUMBER, m_EditTeacherNumber);
	DDX_Control(pDX, IDC_MAXNUMBER, m_EditMaxNumber);
	DDX_Control(pDX, IDC_LESSONNUMBER, m_EditLessonNumber);
	DDX_Control(pDX, IDC_LESSONNAME, m_EditLessonName);
	DDX_Control(pDX, IDC_BEGINDATE, m_EditBeginDate);
	DDX_Text(pDX, IDC_LESSONNAME, m_LessonName);
	DDX_Text(pDX, IDC_LESSONNUMBER, m_LessonNumber);
	DDX_Text(pDX, IDC_MAXNUMBER, m_MaxNumber);
	DDX_Text(pDX, IDC_TEACHERNUMBER, m_TeacherNumber);
	DDX_Text(pDX, IDC_BEGINDATE, m_BeginDate);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CEditLessonPage, CPropertyPage)
	//{{AFX_MSG_MAP(CEditLessonPage)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_DEL, OnDel)
	ON_BN_CLICKED(IDC_EDIT, OnEdit)
	ON_BN_CLICKED(IDC_FIRST, OnFirst)
	ON_BN_CLICKED(IDC_LAST, OnLast)
	ON_BN_CLICKED(IDC_NEXT, OnNext)
	ON_BN_CLICKED(IDC_PREV, OnPrev)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditLessonPage message handlers

BOOL CEditLessonPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	try
	{
//		CString  sDSN="ODBC;DRIVER=Microsoft Access Driver (*.mdb);DSN='';DBQ=telbook.mdb";
		m_database=new CDatabase;
		if(!m_database->Open(_T( "SelectLesson registration" ),FALSE,FALSE, _T( "ODBC;" ),FALSE))
		{
			AfxMessageBox("ODBC Connection Faile!");
//			GetParent()->PostMessage(WM_CLOSE);
			return FALSE;
		}
	
        CString strSQL="select * from LessonInformation";
		m_pLessonset=new CLessonInformationset(m_database);
		m_pLessonset->Open(CRecordset::dynaset,strSQL);

/*        if(isEmpty())
		{
			SetNoRecordBtnState();
			return FALSE;
		}*/
        GetValue();
//		SetButtonState();
	}

	catch(CDBException* pEx)
	{
		pEx->ReportError();
	} 

	return TRUE;  
}

void CEditLessonPage::OnAdd() 
{

//		if(isAppend||isEdit){}//may
	if(m_pLessonset->CanUpdate()!=0)
//	if(m_pLessonset->CanUpdate())
	{
		try
		{
			if(!UpdateData(TRUE)) return;
				UpdateData(TRUE);
		    if(m_LessonNumber==0)
			{ 
				MessageBox("LessonNumber must be inputted","notice!",\
					MB_ICONINFORMATION|MB_OK);
				return;
			}

			//if(isAppend) 
			m_pLessonset->AddNew();
			SetValue();
//			if(m_pLessonset->CanAppend())
//			{
				m_pLessonset->Requery();
				m_pLessonset->MoveLast();
				AfxMessageBox("Add new record succeed");
//			}

//			GetDlgItem(IDC_FIRST)->ShowWindow(SW_SHOW);
//			GetDlgItem(IDC_PREV)->ShowWindow(SW_SHOW);
//			GetDlgItem(IDC_NEXT)->ShowWindow(SW_SHOW);
//			GetDlgItem(IDC_LAST)->ShowWindow(SW_SHOW);
//			SetEditReadOnly();
//			isAppend=FALSE;
//			isEdit=FALSE;

//			GetValue();
//			ClearEditText();
//			SetButtonState();
		}
		catch(CDBException* pEx)
		{
			pEx->ReportError();
		}
	}	
	else
	{
		AfxMessageBox("Can't Append New Record");
	}

/*	else
	{
		if(m_pLessonset->CanAppend())
		{
			GetDlgItem(IDC_FIRST)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_PREV)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_NEXT)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_LAST)->ShowWindow(SW_HIDE);
//			SetButtonText(TRUE);
			m_pLessonset->AddNew();
			
			if(m_LessonNumber==NULL)
			{
				GetDlgItem(IDC_ADD)->EnableWindow(TRUE);
				GetDlgItem(IDC_DEL)->EnableWindow(TRUE);
			}
			
//			SetEditReadOnly();
			ClearEditText();
//			isAppend=TRUE;
			m_EditLessonName.SetFocus();
		}
		else
		{
			AfxMessageBox("Can't Append New Record");
		}
	}*/
}

void CEditLessonPage::OnDel() 
{
	if(m_pLessonset->CanUpdate()!=0)
	{
		m_pLessonset->CancelUpdate();
//		GetDlgItem(IDC_FIRST)->ShowWindow(SW_SHOW);
//		GetDlgItem(IDC_PREV)->ShowWindow(SW_SHOW);
//		GetDlgItem(IDC_NEXT)->ShowWindow(SW_SHOW);
//		GetDlgItem(IDC_LAST)->ShowWindow(SW_SHOW);
//		SetButtonText(FALSE);
//		SetEditReadOnly();

		if(m_pLessonset->IsBOF() && m_pLessonset->IsEOF()) {
//			SetNoRecordBtnState();
			GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
			GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
			GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
			GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
//			GetDlgItem(IDC_EDIT)->EnableWindow(FALSE);
//			GetDlgItem(IDC_DEL)->EnableWindow(FALSE);
			ClearEditText();
			return;}
		else
			GetValue();

//		isEdit=FALSE;
//		isAppend=FALSE;
	
//	if(m_pLessonset->CanUpdate()!=0)
//	{    
//	else 
		if(MessageBox("Really want to delete the record y/n?","Delete the record!",MB_YESNO|MB_ICONINFORMATION) == IDYES)
		{
			m_pLessonset->Delete();
			if(!UpdateData(TRUE)) return;
				UpdateData(TRUE);
			m_pLessonset->MoveNext();
			try
			{
				if(m_pLessonset->IsEOF()){
					m_pLessonset->MovePrev();}
				else{}
				GetValue();
			}
/*			if(isEmpty())
			{
				SetNoRecordBtnState();
				return;
			}
				
//			SetButtonState();
				GetValue();
			}*/
			
			catch(CDBException* pEx){
			pEx->ReportError();}

		}
	}
	else
	{
		AfxMessageBox("Can't Append del the Record!");
	}
}

void CEditLessonPage::OnEdit() 
{
	if(m_pLessonset->CanAppend())
	{
//		GetDlgItem(IDC_FIRST)->ShowWindow(SW_HIDE);
//		GetDlgItem(IDC_PREV)->ShowWindow(SW_HIDE);
//		GetDlgItem(IDC_NEXT)->ShowWindow(SW_HIDE);
//		GetDlgItem(IDC_LAST)->ShowWindow(SW_HIDE);
//		SetButtonText(TRUE);
//		SetEditReadOnly();
//		GetValue();
		m_pLessonset->Edit();
		SetValue();
//		isEdit=TRUE;
//		m_EditLessonName.SetFocus();
		AfxMessageBox("Edit the Record succeed!");
	}
	else
	{
		AfxMessageBox("Can't Modify the Record");
	}
}

void CEditLessonPage::OnFirst() 
{
	GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
	GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
	m_pLessonset->MoveFirst();
	m_pLessonset->MoveNext();
	OnPrev();
}

void CEditLessonPage::OnPrev() 
{
	GetDlgItem(IDC_NEXT)->EnableWindow(TRUE);
	GetDlgItem(IDC_LAST)->EnableWindow(TRUE);
	m_pLessonset->MovePrev();
	if(m_pLessonset->IsBOF())
	{
		GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
		GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
		m_pLessonset->MoveNext();
		return;
	}
	GetValue();
}
void CEditLessonPage::OnLast() 
{
	GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
	GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
	m_pLessonset->MoveLast();
	m_pLessonset->MovePrev();
	OnNext();
}

void CEditLessonPage::OnNext() 
{
	GetDlgItem(IDC_FIRST)->EnableWindow(TRUE);
	GetDlgItem(IDC_PREV)->EnableWindow(TRUE);
	m_pLessonset->MoveNext();
	if(m_pLessonset->IsEOF())
	{
		GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
		GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
		m_pLessonset->MovePrev();
		return;
	}
	GetValue();
}

void CEditLessonPage::GetValue()
{
	CString strTemp;
	//CString valTemp;
	strTemp.Format(_T("%d-%d-%d"),m_pLessonset->m_BeginDate.GetYear(),\
			m_pLessonset->m_BeginDate.GetMonth(),m_pLessonset->m_BeginDate.GetDay());
	//valTemp.Format(_T("%d"),m_BeginDate);

	m_LessonNumber		= m_pLessonset->m_LessonNumber ; 
	m_LessonName		= m_pLessonset->m_LessonName    ;
	m_TeacherNumber	    = m_pLessonset->m_TeacherNumber; 
//	valTemp          = strTemp;
    m_BeginDate			=strTemp;
	m_MaxNumber			= m_pLessonset->m_MaxNumber	  ;
	UpdateData(FALSE);

}
void CEditLessonPage::SetValue()
{
	UpdateData(TRUE);
   
/*	int i=0,year,month,day;
    char temp[3][10]; 
	char string[50];
	strcpy(string,LPCTSTR(m_BeginDate));
	char seps[]   = "-";
	char *token;
 //  printf( "%s\n\nTokens:\n", string );
 //   Establish string and get the first token: 
   token = strtok( string, seps );
   while( token != NULL )
   {
      // While there are tokens in "string"
      //printf( " %s\n", token );
      strcpy(temp[i],token);
      // Get next token: 
      token = strtok( NULL, seps );
	  i++;
   }
    year=atoi(LPCSTR(temp[0]));
    month=atoi(LPCSTR(temp[1]));
    day=atoi(LPCSTR(temp[2]));
//		year=int(temp[0]);
//		month=int(temp[1]);
//		day=int(temp[2]);
//	year=1970;
//	month=5;
//	day=25;

		time_t date1_t;
		tm date_tm;

//    date_tm.tm_hour   =12;
//	   date_tm.tm_min   =0;
	   date_tm.tm_mon   =month;
//	   date_tm.tm_sec   =0;
//	   date_tm.tm_wday   =0; //Day of week (0-6; Sunday = 0)
	   date_tm.tm_mday   =day;
	   date_tm.tm_year   =year;
//	   date_tm.tm_isdst =-1; //Positive if Daylight Saving Time is in effect;
							 //0 if Daylight Saving Time is not in effect; 
							 //Negative if status of DST is unknown.

    date1_t = mktime(&date_tm);
//	CTime t(date1_t);*/
//	CTime t(year, month, day,0,0,0,-1);
//	CTime t(1990,2,8,0,0,0);
//	CString((LPCSTR)m_BeginDate, int nLength);
//CString s( _T("abcdef") );
//ASSERT( s.Mid( 2, 3 ) == _T("cde") );
//ASSERT( s.Left(2) == _T("ab") );
//ASSERT( s.Right(2) == _T("ef") );
//CString str( "cabbage" );
//CString res = str.SpanIncluding( "abc" );
//ASSERT( res == "cabba" );
//res = str.SpanIncluding( "xyz" );
//ASSERT( res.IsEmpty( ) );(LPCSTR)
//src.SpanExcluding( ";,.- :");

	int year,month,day;
	year=atoi((const char*)m_BeginDate.Left(4));
	if(m_BeginDate.Mid( 6, 1 ) == _T("-"))
	{
		month=atoi((const char*)m_BeginDate.Mid( 5, 1 ));
		day=atoi((const char*)m_BeginDate.Mid( 7, m_BeginDate.GetLength()-7));
	}
	else if(m_BeginDate.Mid( 7, 1 ) == _T("-"))
	{
		month=atoi((const char*)m_BeginDate.Mid( 5, 2 ));
		day=atoi((const char*)m_BeginDate.Mid( 8, m_BeginDate.GetLength()-8));
	}
	CTime t(year, month, day,0,0,0,-1);

	m_pLessonset->m_LessonNumber			=   m_LessonNumber;
	m_pLessonset->m_LessonName				=   m_LessonName;
	m_pLessonset->m_TeacherNumber			=   m_TeacherNumber;
	m_pLessonset->m_BeginDate				=   t;
	m_pLessonset->m_MaxNumber				=   m_MaxNumber;
	m_pLessonset->Update();

}
/*void CEditLessonPage::SetButtonState()
{
    if(IsFirstRecord())
	{
		GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
	    GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
	}
	else
	{
		GetDlgItem(IDC_FIRST)->EnableWindow(TRUE);
	    GetDlgItem(IDC_PREV)->EnableWindow(TRUE);

	}

	if(IsLastRecord())
	{	
		GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
	    GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
	}
	else
	{
		GetDlgItem(IDC_NEXT)->EnableWindow(TRUE);
		GetDlgItem(IDC_LAST)->EnableWindow(TRUE);
	}
}*/

BOOL CEditLessonPage::IsFirstRecord()
{
	if(m_pLessonset->IsBOF()&&m_pLessonset->IsEOF())
	{
		return TRUE;
	}
	else if(m_pLessonset->IsBOF())
	{
		m_pLessonset->MoveFirst();
		return TRUE;
	}
	else
		return FALSE;
}

BOOL CEditLessonPage::IsLastRecord()
{
	if(m_pLessonset->IsBOF()&&m_pLessonset->IsEOF())
	{
		return TRUE;
	}
	else if(m_pLessonset->IsEOF())
	{
		m_pLessonset->MoveLast();
		return TRUE;
	}
	else
		return FALSE;
}
/*void CEditLessonPage::SetNoRecordBtnState()
{	
	GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
	GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
	GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
	GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT)->EnableWindow(FALSE);
	GetDlgItem(IDC_DEL)->EnableWindow(FALSE);
	ClearEditText();
}*/
/*BOOL CEditLessonPage::isEmpty()
{
	if(m_pLessonset->IsBOF() && m_pLessonset->IsEOF())
	{
		
		return TRUE;
	}
	else
		return FALSE;

}*/
void CEditLessonPage::ClearEditText()
{
	m_LessonName = _T("");
	m_LessonNumber = 0;
	m_MaxNumber = 0;
	m_TeacherNumber = 0;
	m_BeginDate=_T("");
	UpdateData(FALSE);

}
/*void CEditLessonPage::SetButtonText(BOOL blSet)
{
	if(blSet)
	{
		GetDlgItem(IDC_EDIT)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_ADD)->SetWindowText(_T("OK"));
		GetDlgItem(IDC_DEL)->SetWindowText(_T("Cancel"));
	}
	else
	{
		GetDlgItem(IDC_EDIT)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_ADD)->SetWindowText(_T("Add"));
		GetDlgItem(IDC_DEL)->SetWindowText(_T("Del"));
	}

}*/
/*void CEditLessonPage::SetEditReadOnly()
{
//	GetDlgItem(IDC_FIRST)->ShowWindow(SW_SHOW);
//  GetDlgItem(IDC_PREV)->ShowWindow(SW_SHOW);
//	GetDlgItem(IDC_NEXT)->ShowWindow(SW_SHOW);
//	GetDlgItem(IDC_LAST)->ShowWindow(SW_SHOW);
//	SetButtonText(FALSE);
	m_EditTeacherNumber.SetReadOnly(TRUE);
	m_EditMaxNumber.SetReadOnly(TRUE);
	m_EditLessonNumber.SetReadOnly(TRUE);
	m_EditLessonName.SetReadOnly(TRUE);
	m_EditBeginDate.SetReadOnly(TRUE);
}*/
